﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.EnterpriseServices;
using System.Text;

namespace QT.IIS
{
    /// <summary>
    /// IIS6操作类
    /// </summary>
    public class IIS6
    {
        #region IIS6

        #region 站点操作

        #region 建立站点

        /// <summary>
        /// 创建站点
        /// </summary>
        /// <param name="SiteName">站点名称</param>
        /// <param name="Path">主目录路径</param>
        /// <param name="Domain">主机头</param>
        /// <returns>int,网站标识ID</returns>
        public int CreateWebSite(string SiteName, string Path, string Domain)
        {
            DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
            int sID = 1;
            //获取网站标识ID
            foreach (DirectoryEntry e in root.Children)
            {
                int ID = Convert.ToInt32(e.Name);
                if (ID >= sID)
                {
                    sID = ID + 1;
                }
            }

            //建立站点
            DirectoryEntry mySite = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", sID);
            mySite.Invoke("Put", "ServerComment", SiteName);//网站名称
            mySite.Invoke("Put", "ServerBingding", Domain);//网站主机头
            mySite.Invoke("Put", "ServerState", 2);//1:Starting,2:Started,3:Stopping,4:Stopped,5:Pausing,6:Paused,7:Continuing.默认为4
            mySite.Invoke("Put", "DefaultDoc", "Default.aspx,index.aspx,Default.asp,index.asp,Default.htm,index.html,Default.html,index.html");//文档
            mySite.Invoke("put", "ServerAutoStart", 1);
            mySite.Invoke("SetInfo");
            DirectoryEntry siteDir = mySite.Children.Add("ROOT", "IISWebVirtualDir");
            siteDir.Invoke("AppCreate", true);//创建应用程序站点
            siteDir.CommitChanges();
            mySite.CommitChanges();
            siteDir.Properties["AppIsolated"][0] = 2;//属性指出应用程序是在进程内、进程外还是在进程池中运行。0表示在进程内，1表示在进程外，2表示在进程池
            siteDir.Properties["Path"][0] = Path;//主目录路径
            siteDir.Properties["AccessFlags"][0] = 513;//用于配置文件访问权限标志
            siteDir.Properties["FrontPageWeb"][0] = 1;//服务器实例是否由Microsoft FrontPage处理
            siteDir.Properties["AppRoot"][0] = "/LM/W3SVC" + sID + "/Root";//到应用程序根目录的配置数据库路径
            siteDir.Properties["AppFriendlyName"][0] = "QinTime";//应用程序名
            siteDir.Properties["AspEnableParentPaths"][0] = true;//页面是否允许当前目录的相对路径（使用..\表示法）
            siteDir.CommitChanges();
            mySite.CommitChanges();
            return sID;
        }

        #endregion

        #endregion 

        #region 虚拟目录操作

        #region 创建虚拟目录

        #endregion

        #endregion 

        #region 主机头操作

        #region 绑定主机头

        /// <summary>
        /// 为本地站点绑定主机头
        /// </summary>
        /// <param name="sID">绑定站点ID</param>
        /// <param name="ip">绑定的IP</param>
        /// <param name="port">绑定的端口</param>
        /// <param name="domain">绑定的主机头</param>
        public void Add(int sID, string ip, int port, string domain)
        {
            DirectoryEntry mySite = new DirectoryEntry("IIS://localhost/W3SVC/" + sID);
            PropertyValueCollection serverBindings = mySite.Properties["ServerBindings"];
            string HeaderString = string.Format("{0}:{1}:{2}", ip, port, domain);
            if (!serverBindings.Contains(HeaderString))
            {
                serverBindings.Add(HeaderString);
            }
            else
            {
                QT.Tools.Alert(" 域名已存在，请检查!");
                return;
            }            
            mySite.CommitChanges();
            QT.Tools.Alert("绑定成功！");
        }

        /// <summary>
        /// 为本地站点绑定主机头
        /// </summary>
        /// <param name="sID">绑定站点ID</param>
        /// <param name="ip">绑定IP</param>
        /// <param name="domain">绑定域名</param>
        public void Add(int sID, string ip, string domain)
        {
            DirectoryEntry mySite = new DirectoryEntry("IIS://localhost/W3SVC/" + sID);
            PropertyValueCollection serverBindings = mySite.Properties["ServerBindings"];
            string HeaderString = string.Format("{0}:{1}:{2}", ip, "80", domain);
            if (!serverBindings.Contains(HeaderString))
            {
                serverBindings.Add(HeaderString);
            }
            else
            {
                QT.Tools.Alert(" 域名已存在，请检查!");
                return;
            }
            mySite.CommitChanges();
            QT.Tools.Alert("绑定成功！");
        }

        /// <summary>
        /// 为本地站点绑定主机头
        /// </summary>
        /// <param name="sID">绑定站点ID</param>
        /// <param name="domain">绑定域名</param>
        public void Add(int sID, string domain)
        {
            DirectoryEntry mySite = new DirectoryEntry("IIS://localhost/W3SVC/" + sID);
            PropertyValueCollection serverBindings = mySite.Properties["ServerBindings"];
            string HeaderString = string.Format("{0}:{1}:{2}", "*", "80", domain);
            if (!serverBindings.Contains(HeaderString))
            {
                serverBindings.Add(HeaderString);
            }
            else
            {
                QT.Tools.Alert(" 域名已存在，请检查!");
                return;
            }
            mySite.CommitChanges();
            QT.Tools.Alert("绑定成功！");
        }

        #endregion

        #region 删除主机头

        /// <summary>
        /// 删除本地站点主机头
        /// </summary>
        /// <param name="sID">绑定站点ID</param>
        /// <param name="ip">绑定的IP</param>
        /// <param name="port">绑定的端口</param>
        /// <param name="domain">绑定的主机头</param>
        public void Remove(int sID, string ip, int port, string domain)
        {
            DirectoryEntry mySite = new DirectoryEntry("IIS://localhost/W3SVC/" + sID);
            PropertyValueCollection serverBindings = mySite.Properties["ServerBindings"];
            string HeaderString = string.Format("{0}:{1}:{2}", ip, port, domain);
            if (serverBindings.Contains(HeaderString))
            {
                serverBindings.Remove(HeaderString);
            }
            else
            {
                QT.Tools.Alert(" 域名不存在，请检查!");
                return;
            }
            mySite.CommitChanges();
            QT.Tools.Alert("删除成功！");
        }

        /// <summary>
        /// 删除本地站点主机头
        /// </summary>
        /// <param name="sID">绑定站点ID</param>
        /// <param name="ip">绑定IP</param>
        /// <param name="domain">绑定域名</param>
        public void Remove(int sID, string ip, string domain)
        {
            DirectoryEntry mySite = new DirectoryEntry("IIS://localhost/W3SVC/" + sID);
            PropertyValueCollection serverBindings = mySite.Properties["ServerBindings"];
            string HeaderString = string.Format("{0}:{1}:{2}", ip, "80", domain);
            if (serverBindings.Contains(HeaderString))
            {
                serverBindings.Remove(HeaderString);
            }
            else
            {
                QT.Tools.Alert(" 域名不存在，请检查!");
                return;
            }
            mySite.CommitChanges();
            QT.Tools.Alert("删除成功！");
        }

        /// <summary>
        /// 删除本地站点主机头
        /// </summary>
        /// <param name="sID">绑定站点ID</param>
        /// <param name="domain">绑定域名</param>
        public void Remove(int sID, string domain)
        {
            DirectoryEntry mySite = new DirectoryEntry("IIS://localhost/W3SVC/" + sID);
            PropertyValueCollection serverBindings = mySite.Properties["ServerBindings"];
            string HeaderString = string.Format("{0}:{1}:{2}", "*", "80", domain);
            if (serverBindings.Contains(HeaderString))
            {
                serverBindings.Remove(HeaderString);
            }
            else
            {
                QT.Tools.Alert(" 域名不存在，请检查!");
                return;
            }
            mySite.CommitChanges();
            QT.Tools.Alert("删除成功！");
        }

        #endregion

        #region 获取主机头

        /// <summary>
        /// 获取主机头
        /// </summary>
        /// <param name="sID">网站表示ID</param>
        /// <returns>主机头列表</returns>
        public string[] List(int sID)
        {
            DirectoryEntry mySite = new DirectoryEntry("IIS://localhost/W3SVC/" + sID);
            PropertyValueCollection serverBindings = mySite.Properties["ServerBindings"];
            string[] DomainList = null;
            
            for(int i=0;i<serverBindings.Count;i++)
            {
                DomainList[i] += serverBindings[i].ToString();
            }

            return DomainList;
        }

        #endregion

        #endregion

        #endregion

        public void DomainBind7(int sID, string ip, int port, string domain)
        {
            //ServerManager iisManager = new ServerManager();
            //iisManager.Sites["Default Web Site"];
            //iisManager.Update(); 
        }
    }
}
